package com.db.logging.demo;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;

/**
 * @author nulls.narik
 */
public class HelloWorld {


    public static void main(String[] args) throws InterruptedException {
        final Logger loggerRoot = LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME);
        if (loggerRoot.isDebugEnabled()) {
            loggerRoot.debug("Start test");
        }
        final Logger logger = LoggerFactory.getLogger(HelloWorld.class);


        final AtomicBoolean stop = new AtomicBoolean(false);
        final CountDownLatch countDownLatch = new CountDownLatch(1);
        int countThread = 3;
        ExecutorService executorService = Executors.newFixedThreadPool(countThread * 3);
        for (int i = 0; i < countThread; i++) {
            executorService.submit(new Runnable() {
                @Override
                public void run() {
                    try {
                        countDownLatch.await();
                        int i = 0;
                        while (!stop.get()) {
                            logger.debug("some log message: " + i++);
                            Thread.sleep(17);
                        }
                    } catch (InterruptedException e) {
                        Thread.currentThread().interrupt();
                    }
                }
            });
        }
        for (int i = 0; i < countThread; i++) {
            executorService.submit(new Runnable() {
                @Override
                public void run() {
                    try {
                        countDownLatch.await();
                        int i = 0;
                        while (!stop.get()) {
                            logger.info("some log message: %d",  i++);
                            Thread.sleep(7);
                        }
                    } catch (InterruptedException e) {
                        Thread.currentThread().interrupt();
                    }
                }
            });
        }
        for (int i = 0; i < countThread; i++) {
            executorService.submit(new Runnable() {
                @Override
                public void run() {
                    try {
                        countDownLatch.await();
                        int i = 0;
                        while (!stop.get()) {
                            logger.error("some log message: " + i++);
                            Thread.sleep(13);
                        }
                    } catch (InterruptedException e) {
                        Thread.currentThread().interrupt();
                    }
                }
            });
        }
        countDownLatch.countDown();


        Thread.sleep(1000);
        stop.set(true);
        executorService.shutdown();
        if (loggerRoot.isDebugEnabled()) {
            loggerRoot.debug("Done test");
        }
    }

}
